package com.lkm.app_ui.jz.listener;

import android.annotation.SuppressLint;
import android.content.Context;

import com.king.app.updater.callback.UpdateCallback;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.RootCommand;
import com.lkm.app_ui.jz.util.Print;

import java.io.File;

public class MyUpdateCallback implements UpdateCallback {

    @SuppressLint("InflateParams")
    @Override
    public void onDownloading(boolean isDownloading) {
        if (!isDownloading) {
            Print.showToast("开始下载...");
        }
    }

    @Override
    public void onStart(String url) {
        updateProgress(0, 100);
    }

    @Override
    public void onProgress(long progress, long total, boolean isChange) {
        if (isChange) {
            updateProgress(progress, total);
        }
    }

    @Override
    public void onFinish(File file) {
        //AppDialog.INSTANCE.dismissDialog();
        Print.d("APK下载完毕：" + file.getPath() + ",isXIANGCHENG:" + RunTimeData.isXIANGCHENG);
//        AppHelp.installApk(file.getPath());
        RootCommand.runT("/system/bin/pm install -r " + file.getPath());
    }

    @Override
    public void onError(Exception e) {
        //AppDialog.INSTANCE.dismissDialog();
        Print.showToast("APK更新失败->" + e.toString());
    }

    @Override
    public void onCancel() {
        //AppDialog.INSTANCE.dismissDialog();
    }

    @SuppressLint("SetTextI18n")
    private void updateProgress(long progress, long total) {
        Context context = MyApplication.getInstance();
        if (context == null) return;
        if (progress > 0) {
            int currProgress = (int) (progress * 1.0f / total * 100.0f);
            Print.showToast(context.getString(R.string.app_updater_progress_notification_content) + currProgress + "%");
        } else {
            Print.showToast(context.getString(R.string.app_updater_start_notification_content));
        }
    }
}
